// 切换点击运行
import store from "../store/index.js";
import {computed} from "vue";

const play = (songIfno) => {
    store.dispatch("changeSong", songIfno);
    let audio = document.getElementById('audio');
    audio.play();
    store.commit("setIsPlay", true);
}


let isPlay = computed(() => store.state.index.isPlay);
let timer = null;
// 播放与暂停音频按钮
const playAudio = () => {
    let audio = document.getElementById('audio');
//   src 歌曲的路径
//   preload 是否在页面加载后立即加载（设置 autoplay 后无效）
//   controls 显示 audio 自带的播放控件
//   loop 音频循环
//   autoplay 音频加载后自动播放
//   currentTime 音频当前播放时间
//   duration 音频总长度
//   ended 音频是否结束
//   muted 音频静音为 true
//   volume 当前音频音量
//   readyState 音频当前的就绪状态
    clearInterval(timer);
    if (!isPlay.value) {
        store.commit("SETCTIME",Math.round(audio.currentTime));
        store.commit("SETETIME",Math.round(audio.duration));
        store.commit("setIsPlay", true);
        timer = setInterval(() => {
            store.commit("SETCTIME", Math.round(audio.currentTime));
            store.commit("SETETIME", Math.round(audio.duration));
            console.log(audio.currentTime, audio.duration)
            if (audio.currentTime === audio.duration) {
                if (store.state.index.songList.indexOf(store.state.index.songInfo) === store.state.index.songList.length - 1) {
                    store.dispatch("changeSong", store.state.index.songList[0])
                    return;
                }
                store.dispatch("changeSong", store.state.index.songList[store.state.index.songList.indexOf(store.state.index.songInfo) + 1])
            }
        }, 1000)
        audio.play();
    } else {
        store.commit("setIsPlay", false);

        audio.pause();
    }

}
// 点击就播放
const playAudio2 = () => {
    let audio = document.getElementById('audio');
    audio.currentTime = 0;
    store.commit("SETCTIME",0);
    store.commit("SETETIME",Math.round(audio.duration));
//   src 歌曲的路径
//   preload 是否在页面加载后立即加载（设置 autoplay 后无效）
//   controls 显示 audio 自带的播放控件
//   loop 音频循环
//   autoplay 音频加载后自动播放
//   currentTime 音频当前播放时间
//   duration 音频总长度
//   ended 音频是否结束
//   muted 音频静音为 true
//   volume 当前音频音量
//   readyState 音频当前的就绪状态
    clearInterval(timer);
    store.commit("setIsPlay", true);
    timer = setInterval(() => {
        store.commit("SETCTIME", Math.round(audio.currentTime));
        store.commit("SETETIME", Math.round(audio.duration));
        console.log(audio.currentTime, audio.duration)
        if (audio.currentTime === audio.duration) {
            if (store.state.index.songList.indexOf(store.state.index.songInfo) === store.state.index.songList.length - 1) {
                store.dispatch("changeSong", store.state.index.songList[0])
                return;
            }
            store.dispatch("changeSong", store.state.index.songList[store.state.index.songList.indexOf(store.state.index.songInfo) + 1])
        }
    }, 1000)
    audio.play();

}
const methods = {
    play,
    playAudio,
    playAudio2
}
export default methods;